//
//  WBNewFeatureView.swift
//  weibo
//
//  Created by 林帆 on 2017/5/26.
//  Copyright © 2017年 林帆. All rights reserved.
//

import UIKit

class WBNewFeatureView: UIView {

    @IBOutlet weak var ScrollView0527: UIScrollView!
    @IBOutlet weak var EnterButton: UIButton!
    @IBOutlet weak var PageControl0527: UIPageControl!
    
    @IBAction func enterStatus(_ sender: UIButton) {
    }
  
    class func NewFeatureView( ) -> WBNewFeatureView {
        let nib  = UINib(nibName: "WBNewFeatureView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBNewFeatureView
        
        // 从 XIB 加载的视图 默认是600*600
        v.frame = UIScreen.main.bounds
        
        return v
        
    }
    
    override func awakeFromNib() {
        
        // 如果使用自动布局设置的界面, 从XIB 加载默认是 600*600 大小
        // 添加 4 个 图像视图
        let count = 4
        let rect = UIScreen.main.bounds
        
        for i in 0..<count {
            let imageN = "foot_green_icon"
            let iv = UIImageView(image: UIImage(named: imageN))
            
            // 偏移量
            iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width , dy: 0)
            
            ScrollView0527.addSubview(iv)
        }
        
        ScrollView0527.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        ScrollView0527.bounces = false
        ScrollView0527.isPagingEnabled = true
        ScrollView0527.showsVerticalScrollIndicator = false
        ScrollView0527.showsHorizontalScrollIndicator = false
        ScrollView0527.delegate = self
        // 影藏按钮
        EnterButton.isHidden = true
    }
    
}

extension WBNewFeatureView : UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        // 滚动到最后一屏 ,让视图删除
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        // 判断是否最后一页
        if page == scrollView.subviews.count {
            print("热烈欢迎")
            removeFromSuperview()
        }
        // 如果是倒数第二页 显示按钮
        EnterButton.isHidden = (page != scrollView.subviews.count - 1)
        
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // 一旦滚动隐藏 按钮
        EnterButton.isHidden = true
        // 计算当前的偏移量
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        // 设置分页控件
        PageControl0527.currentPage = page
        //分页控件的隐藏
        PageControl0527.isHidden = (page == scrollView.subviews.count)
    }
    
}
